require(["jquery","paging","template","bootstrap","echarts","bbcare","scprit/department/view/template/follow-up/common.js"],function($,Paging,template,bootstrap,echarts,$bb,$common) {
	'use strict';
	var URL = $bb.baseUrl("department")+"followup/service";
	var obj=$bb.getPageParams();
	var userId=obj.babyId;
	var type=obj.type;//历史记录传参；
	var HashMap=$common.hashmap();
	var count =1, pagesize =15;//历史记录分页；
	var account=eval('('+sessionStorage.account+')');
	var accountName=account.alias;
	$("#nowTime").html($common.getNowFormatDate());
	$("#userName").html(accountName);//检查医生名字

	var btnSubmit=$("#submitData");
	$(".mask, .AddInformation").hide();
	getBabyInfo();
	getAllPostion();

	$("#historyData").click(function(event) {
		getBabyList(1, pagesize);
		var p = new Paging();
		$("#pageTool").html("");
		p.init({target:'#pageTool',pagesize:pagesize,count:count,toolbar:false,callback:function(page,size,count){
			getBabyList(page,size);
		}});
		$(".mask, .AddInformation").show();
	});
	$(".AddInformation h4 span").click(function(event) {
		$(".mask, .AddInformation").hide();
	});

	$("div.text").hide();
	$(".box h3").click(function(){
		$(this).next(".text").slideToggle("slow");
	});
	function getBabyInfo() {
		$bb.ajax({
			url: URL+"/patient/patientInfoPtId",
			data: JSON.stringify({
				data: {
					"patientId":userId
				}
			}),
			success: function (data) {
				if(data.success) {
					var babyInfo=data.data.PatientInfo;
					var babyData=$common.babyInfoDeal(babyInfo);
					$common.infoInit({"id":"#babyInfoList","data":babyData});
				}
			}
		});
	}
	function getAllPostion() {
		$bb.ajax({
			url: URL+"/patient/evaluation/getAllPostion",
			async: false,
			data: JSON.stringify({
				data: {
					"examinationPaperName" : type,//评测类型；
					"userId":userId
				}
			}),
			success: function(data){
				if(data.success) {
					var content = '';
					var num = 1;
					$.each(data.data, function(id,value) {
						if(id != "success"){
							content+= '<div class="box"><h3>'+id+'</h3><div class="text">';
							$.each(value, function(id1,value1) {
								var num = id1*1.0+1;
								content+= '<li><a>'+num+"."+value1.name+'</a><br/>';
								if(id!="动物房" && id!="译码") {
									var strs= new Array();
									strs=value1.score.split(",");
									if(strs.length*1==1) {
										for(var i=0;i<=value1.score*1;i++) {
											content+='<label><input type="radio" name="'+id+"."+num+'" value="'+i+'"/>'+i+'</label>';
										}
									} else {
										for(var i=0;i<=strs.length*1;i++) {
											if(typeof(strs[i]) != "undefined"){
												content+='<label><input type="radio" name="'+id+"."+strs[i]+'" value="'+strs[i]+'"/>'+strs[i]+'</label>';
											}
										}
									}
								} else {
									content+= '<div style="float:right; text-align:right;"><a>'+id+'评分：</a><select id="zooId" style="width:100px;">';
									for(var i=0;i<=value1.score*1;i++) {
										content+='<option value="'+i+'">'+i+'</option>';
									}
									content+='</select></div>';
								}
							});
							content+='</li></div></div>';
						}
					});
					$("#postion ul").html(content);
				}
			}
		});
	}
	btnSubmit.click(function(){
		var commonSense=0,zoo=0,vocabulary=0,picture=0,arithmetic=0,maze=0,geometry=0,similar=0,pattern=0,understand=0;
		HashMap.prototype.constructor = HashMap;
		var hashMap = new HashMap();
		$("#postion ul :checked").each(function () {
			if(typeof($(this).attr("name")) != "undefined"){
				var arr = $(this).attr("name").split('.');
				if(arr.length>1) {
					if(arr[0]=='常识')    commonSense += $(this).val()*1.0;
					if(arr[0]=='动物房')	  zoo += $(this).val()*1.0;
					if(arr[0]=='词汇')    vocabulary += $(this).val()*1.0;
					if(arr[0]=='图画补缺')picture += $(this).val()*1.0;
					if(arr[0]=='算术题')  arithmetic += $(this).val()*1.0;
					if(arr[0]=='迷宫')    maze += $(this).val()*1.0;
					if(arr[0]=='几何图形')geometry += $(this).val()*1.0;
					if(arr[0]=='类同词')  similar += $(this).val()*1.0;
					if(arr[0]=='木块图案')pattern += $(this).val()*1.0;
					if(arr[0]=='理解')    understand += $(this).val()*1.0;
				}
			}
		});
		zoo=$("#zooId").val();
		hashMap.put('常识', commonSense);
		hashMap.put('动物房', zoo*1.0);
		hashMap.put('词汇', vocabulary);
		hashMap.put('图画补缺', picture);
		hashMap.put('算术题', arithmetic);
		hashMap.put('迷宫', maze);
		hashMap.put('几何图形', geometry);
		hashMap.put('类同词', similar);
		hashMap.put('木块图案', pattern);
		hashMap.put('理解', understand);
		$bb.ajax({
			url: URL+"/patient/evaluation/getScore",
			data: JSON.stringify({
				data: {
					"userId" : userId,
					"examinationPaperName" : "Small"+type,
					"postionIds" : JSON.stringify(hashMap)
				}
			}),
			success: function(data){
				if(data.success) {
					localStorage.setItem("smallWechsler",JSON.stringify(data.data));
					$bb.openPage("smallWechslerResult",obj);
				}
			}
		});
	});

	//历史记录列表点击：
	$("#dataList").on('click',"ul[result]",function () {
		var data = eval("("+hashMap.get($(this).attr("result")).replace(/\=/g,"\:")+")");
		localStorage.setItem("smallWechsler",JSON.stringify(data));
		$bb.openPage("smallWechslerResult",obj);
	});
	//历史记录列表：
	var hashMap = new HashMap();
	function getBabyList(page, rows) {
		$bb.ajax({
			url: URL+"/patient/evaluation/getHistoryScore",//baby地址
			async: false,
			data: JSON.stringify({
				data: {
					"userId": userId,
					"examinationPaperName": "Small"+type,
					"page": page,
					"rows": rows
				}
			}),
			success: function (data) {
				if (data.success) {
					hashMap = new HashMap();
					count = data.total;
					var babyData=data.rows;
					if(count==0){
						$bb.toastShow("患者还未参与该项测评！");
					}
					var str = '';
					for (var i = 0; i < data.rows.length; i++) {
						hashMap.put(data.rows[i].answerId, data.rows[i].result);
						var pretermBirth = 37 * 7 > data.rows[i].pregnancyWeek ? "是" : "否";
						var nowDate =$common.getNowFormatDate();
						var monthAge = $common.DateDiff(nowDate, data.rows[i].birthday) / 30;
						var weight= data.rows[i].birthWeight==undefined?"未知":data.rows[i].birthWeight+"(g)";
						var doctorName= data.rows[i].doctorName==undefined?"未知":data.rows[i].doctorName;

						str += '<ul class="clearfix" result="' + data.rows[i].answerId + '">';
						str += '<li>' + data.rows[i].babyName + '</li>';
						str += '<li>' + weight+ '</li>';
						str += '<li>' + data.rows[i].birthday + '</li>';
						str += '<li>' + data.rows[i].pregnancyWeek + '(天)</li>';
						str += '<li>' + pretermBirth + '</li>';
						str += '<li>' + doctorName + '</li>';
						str += '<li>' + data.rows[i].createTime + '</li>';
						str += '</ul>';
					}
					$("#dataList").html(str);
				}
			}
		});
	}

})

